package com.shujia.mapreduce;

import org.apache.hadoop.fs.Path;
import org.apache.hadoop.io.IntWritable;
import org.apache.hadoop.io.LongWritable;
import org.apache.hadoop.io.NullWritable;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.Job;
import org.apache.hadoop.mapreduce.Mapper;
import org.apache.hadoop.mapreduce.Reducer;
import org.apache.hadoop.mapreduce.lib.input.FileInputFormat;
import org.apache.hadoop.mapreduce.lib.output.FileOutputFormat;

import java.io.IOException;

public class Demo6Combiner {

    public static class CombinerMapper extends Mapper<LongWritable, Text, Text, IntWritable> {
        @Override
        protected void map(LongWritable key, Text value, Mapper<LongWritable, Text, Text, IntWritable>.Context context) throws IOException, InterruptedException {

            //获取一行数据并转换成字符串
            String line = value.toString();

            //切分一行字符串，获取班级
            String clazz = line.split(",")[4];

            //输出班级到reduce段
            context.write(new Text(clazz), new IntWritable(1));

        }
    }

    public static class Combiner extends Reducer<Text, IntWritable, Text, IntWritable> {
        /**
         * Combiner：在map端执行完之后执行，在map端进行预聚合
         */
        @Override
        protected void reduce(Text key, Iterable<IntWritable> values, Reducer<Text, IntWritable, Text, IntWritable>.Context context) throws IOException, InterruptedException {

            //统计班级的人数
            int sum = 0;
            for (IntWritable value : values) {
                int i = value.get();
                sum += i;
            }

            //输出班级和统计的班级人数
            context.write(new Text(key), new IntWritable(sum));

        }
    }

    public static class CombinerReducer extends Reducer<Text, IntWritable, Text, NullWritable> {
        @Override
        protected void reduce(Text key, Iterable<IntWritable> values, Reducer<Text, IntWritable, Text, NullWritable>.Context context) throws IOException, InterruptedException {

            int sum = 0;
            for (IntWritable value : values) {
                int i = value.get();
                sum += i;
            }

            //key是班级转换成字符串，并拼接统计的班级人数
            String clazzNum = key.toString() + "," + sum;

            //将拼接的数据输出到hdfs
            context.write(new Text(clazzNum), NullWritable.get());

        }
    }

    public static void main(String[] args) throws Exception {

        Job job = Job.getInstance();

        job.setJobName("combiner");

        job.setNumReduceTasks(1);

        //指定main所在类的类名
        job.setJarByClass(Demo6Combiner.class);

        //指定map端
        job.setMapperClass(CombinerMapper.class);
        job.setMapOutputKeyClass(Text.class);
        job.setMapOutputValueClass(IntWritable.class);

        //指定Combiner
        job.setCombinerClass(Combiner.class);

        //指定reduce端
        job.setReducerClass(CombinerReducer.class);
        job.setOutputKeyClass(Text.class);
        job.setOutputValueClass(NullWritable.class);

        FileInputFormat.addInputPath(job, new Path("/data/student"));

        FileOutputFormat.setOutputPath(job, new Path("/data/clazz_num"));

        job.waitForCompletion(true);

    }
}
